package com.youfan.control;

import com.youfan.entity.Useradmin;
import com.youfan.page.CommonPage;
import com.youfan.re.Result;
import com.youfan.service.UseradminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "useradmin")
public class UseradminControl {

    @Autowired
    private UseradminService useradminService;

    @RequestMapping(method = RequestMethod.POST,value = "add")
    @ResponseBody
    public Result add(@RequestBody Useradmin useradmin)
    {
        try {
            int sucessnums = useradminService.insertOne(useradmin);
            return Result.success(sucessnums,"添加成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("添加失败");
        }
    }

    @RequestMapping(method = RequestMethod.POST,value = "view")
    @ResponseBody
    public Result view(@RequestBody Useradmin useradmin)
    {
        try {
            Useradmin useradminResult = useradminService.findByid(useradmin);
            return Result.success(useradminResult,"查询单个对象成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("查询单个对象失败");
        }
    }

    @RequestMapping(method = RequestMethod.POST,value = "update")
    @ResponseBody
    public Result update(@RequestBody Useradmin useradmin)
    {
        try {

            Useradmin useradmin1 = useradminService.findByid(useradmin);
            useradmin1.setUsername(useradmin.getUsername());
            useradmin1.setPassword(useradmin.getPassword());
            useradmin1.setIcon(useradmin.getIcon());
            useradmin1.setEmail(useradmin.getEmail());
            useradmin1.setNickName(useradmin.getNickName());
            useradmin1.setNoteinfo(useradmin.getNoteinfo());
            useradmin1.setStatus(useradmin.getStatus());
            int result = useradminService.updateOne(useradmin1);
            return Result.success(result,"更新成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("更新失败");
        }
    }


    @RequestMapping(method = RequestMethod.POST,value = "list")
    @ResponseBody
    public Result list(@RequestBody Useradmin useradmin){
        try {
            if (useradmin.getPageNum() == null) {
                useradmin.setPageNum(1);
            }
            if (useradmin.getPageSize() == null) {
                useradmin.setPageSize(5);
            }
            CommonPage<Useradmin> resultdata = useradminService.queryByCondtion(useradmin);

            return Result.success(resultdata, "查询成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("查询失败"+e.getMessage());
        }
    }



}
